﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Configuration;

namespace YanForum.Engine.Services
{
    public static class Common
    {
        public static string GetFtpUser()
        {
            return YanForum.Engine.Services.BaseService.FtpUserUploadImg;
        }

        public static string GetFtpPass()
        {
            return YanForum.Engine.Services.BaseService.FtpPassUploadImg;
        }

        public static string GetDomain()
        {
            return YanForum.Engine.Services.BaseService.GetDomain;
        }

        public static string GetFolderByDate(string strRoot)
        {
            if (strRoot.Substring(strRoot.Length - 1) != "/")
                strRoot += "/";
            return GetFullFolder(strRoot);
        }
        public static string GetFullFolder(string strRoot)
        {
            string strDate = DateTime.Now.Year.ToString() + DateTime.Now.ToString("MM");
            strRoot += strDate;
            return strRoot;
        }

        public static void FtpCreateFolder(string strFolder, string ftpUser, string ftpPassword)
        {
            try
            {
                FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(strFolder);
                ftp.Method = WebRequestMethods.Ftp.MakeDirectory;
                ftp.Credentials = new NetworkCredential(ftpUser, ftpPassword);
                ftp.UsePassive = false;
                FtpWebResponse CreateForderResponse = (FtpWebResponse)ftp.GetResponse();
            }
            catch (Exception ex)
            {
            }
        }

        public static Byte[] SaveImage(Bitmap img, long quality)
        {
            EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality);
            ImageCodecInfo jpegCodec = getEncoderInfo("image/jpeg");

            if (jpegCodec == null)
                return null;

            EncoderParameters encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = qualityParam;
            MemoryStream objStream = new MemoryStream();
            img.Save(objStream, jpegCodec, encoderParams);
            return objStream.ToArray();
        }

        private static ImageCodecInfo getEncoderInfo(string mimeType)
        {
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            for (int i = 0; i < codecs.Length; i++)
                if (codecs[i].MimeType == mimeType)
                    return codecs[i];
            return null;
        }

        public static void UpFileServer(string strUrl, Byte[] buffer)
        {
            try
            {
                FtpWebRequest requestObj = (FtpWebRequest)FtpWebRequest.Create(strUrl);
                requestObj.Method = WebRequestMethods.Ftp.UploadFile;
                requestObj.UseBinary = true;
                requestObj.KeepAlive = false;
                requestObj.Credentials = new NetworkCredential(GetFtpUser(), GetFtpPass());
                Stream requestStream = requestObj.GetRequestStream();
                requestStream.Write(buffer, 0, buffer.Length);

                requestStream.Flush();
                requestStream.Close();
                requestObj = null;
            }
            catch (Exception ex)
            {
            }
        }
        public static void CropUploadImage(string strUrl, string strFtp, int intWidth, int intHeight, int intX, int intY)
        {
            byte[] arrResult = new byte[1];

            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)System.Net.HttpWebRequest.Create(strUrl);
                webRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
                webRequest.AllowWriteStreamBuffering = true;
                int intNWidth = intWidth;
                int intNHeight = intHeight;
                WebResponse webResponse = webRequest.GetResponse();
                Stream stream = webResponse.GetResponseStream();
                
                using (Image image = Image.FromStream(stream))
                {
                    using (Bitmap bmp = new Bitmap(intNWidth, intNHeight))
                    {
                        bmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                        using (Graphics Graphic = Graphics.FromImage(bmp))
                        {
                            Graphic.SmoothingMode = SmoothingMode.AntiAlias;
                            Graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            Graphic.PixelOffsetMode = PixelOffsetMode.HighQuality;
                            Graphic.DrawImage(image, new Rectangle(0, 0, intNWidth, intNHeight), intX, intY, intWidth, intHeight, GraphicsUnit.Pixel);
                            MemoryStream ms = new MemoryStream();
                            bmp.Save(ms, image.RawFormat);
                            arrResult = ms.GetBuffer();
                        }
                    }
                }

                webResponse.Close();
            }
            catch (Exception Ex)
            {
                throw (Ex);
            }

            UpFileServer(strFtp, arrResult);
        }
    }
}
